package com.project.x;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

/**
 * Created by Miko Arce on 2/20/2015.
 */

public class FileHandler {

    private Context context;

    public FileHandler(Context context){
        this.context = context;
    }

    public static boolean canWriteOnExternalStorage() {
        // get the state of your external storage
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            // if storage is mounted return true
            Log.v("STORAGE", "Yes, can write to external storage.");
            return true;
        }
        Log.v("STORAGE", "NO, cannot write to external storage.");
        return false;
    }

    public void formatMessageEntryToTextFile(ArrayList<MessageEntry> messageEntryArrayList,
                                             OutputStreamWriter osw) throws IOException {
        MessageEntry messageEntry;
        for (int i = 0; i < messageEntryArrayList.size(); i++){
            osw.write("MSG_ENTRY\n");
            messageEntry = messageEntryArrayList.get(i);
            osw.write("ID:" + messageEntry.getId() + "\n");
            osw.write("DATETIME:" + messageEntry.getDateTime() + "\n");
            osw.write("TYPE:" + messageEntry.getType() + "\n");
            osw.write("BODY:" + messageEntry.getMessageContent() + "\n");
        }
    }

    public void formatMessageThreadToTextFile(MessageThread messageThread, OutputStreamWriter osw)
            throws IOException{
        int log = Log.d("MSG_THREAD", "Writing for ThreadID: " + messageThread.getId() +
                ", Entry Size: " + messageThread.getMessageEntryList().size());
        osw.write("THREAD_INFO\n");
        osw.write("THREAD_ID:" + messageThread.getId() + "\n");
        osw.write("ADDRESS:"+ messageThread.getPhoneNumber() + "\n");
        osw.write("FIRST_NAME:" + messageThread.getFirstName() + "\n");
        osw.write("LAST_NAME" + messageThread.getLastName() + "\n");
        formatMessageEntryToTextFile(messageThread.getMessageEntryList(), osw);
    }

    public void initializeThreadFile(ArrayList<MessageThread> messageThreadArrayList){
        File sdcard = Environment.getExternalStorageDirectory();
        //File dir = new File(sdcard.getAbsolutePath() + "/projectx_message/");
        File dir = new File("/sdcard/ProjectX/msg");
        dir.mkdirs();

        for (int i = 0; i < messageThreadArrayList.size(); i++){
            MessageThread messageThread = messageThreadArrayList.get(i);
            File file = new File(dir, Integer.toString(messageThread.getId()));

            try{
                OutputStreamWriter outputStreamWriter;
                if(!file.exists()) {
                    int log = Log.v("FILE_WRITE","File does NOT exist: " + Integer.toString(messageThread.getId()));

                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    outputStreamWriter = new OutputStreamWriter(fileOutputStream);
                    formatMessageThreadToTextFile(messageThread, outputStreamWriter);
                    outputStreamWriter.close();
                }
                else {
                    int log = Log.v("FILE_WRITE","File exists: " + Integer.toString(messageThread.getId()));
                }
            } catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
    }

}
